package com.ls.fw.data.search.impl.dao;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.HttpClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.SortClause;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.impl.HttpClientUtil;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.LBHttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.CursorMarkParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.server.support.HttpSolrServerFactory;
import org.springframework.data.solr.server.support.MulticoreSolrServerFactory;

import com.ls.fw.data.bean.Column;
import com.ls.fw.data.bean.Record;
import com.ls.fw.data.bean.Table;
import com.ls.fw.data.dao.DaoAdapter;
import com.ls.fw.data.enums.AFunctions;
import com.ls.fw.data.exception.DataException;
import com.ls.fw.data.mapper.ScanRowMapper;
import com.ls.fw.data.mapper.impl.ScanRowMapperAdapter;
import com.ls.fw.data.result.DataResult;
import com.ls.fw.data.result.DataResultSet;
import com.ls.fw.data.result.DataResultSetImpl;
import com.ls.fw.data.search.impl.solrj.maker.DataResultMaker;
import com.ls.fw.data.search.impl.solrj.statement.DeleteParser;
import com.ls.fw.data.search.impl.solrj.statement.SelectParser;
import com.ls.fw.data.statement.Delete;
import com.ls.fw.data.statement.Insert;
import com.ls.fw.data.statement.Select;
import com.ls.fw.data.statement.Update;
import com.ls.fw.data.utils.IDHander;
import com.ls.fw.data.visitor.FieldVisitor;
import com.ls.fw.data.visitor.RecordVisitor;

/**
 * solr实现
 * 
 * @author ls 2014年12月1日上午9:57:31
 */
@SuppressWarnings({ "unchecked" })
public abstract class SearchDaoAdapter extends DaoAdapter implements SearchDao {
	
	protected static final String DEFAULT_COLLECTION = "collection1";
	protected static final int DEFAULT_COMMIT_MS = 10;

	protected String name = dataConfig.getServerName();// 服务名称
	
	// solr服务
	protected SolrServer server = null;
	
	protected SolrTemplate template; 

	protected MulticoreSolrServerFactory coreFactory;
	
	protected HttpSolrServerFactory factory = null; 
	
	public SearchDaoAdapter() {
		super();
	}

	public SearchDaoAdapter(String address) {
		this.address = address;
	}

	/**
	 * 
	 * @param address
	 *            地址
	 * @param autoCommit
	 *            是否自动提交
	 */
	public SearchDaoAdapter(String address, 
			boolean autoCommit) {
		super();
		this.address = address;
		this.autoCommit = autoCommit;
	}


	public SearchDaoAdapter(SolrServer server) {
		super();
		this.server = server;
		init();
	}

	public SearchDaoAdapter(String name, SolrServer server,
			SolrTemplate template) {
		super();
		this.name = name;
		this.server = server;
		this.template = template;
	}

	private void init(){
		if(template == null && this.server != null){
			template = new SolrTemplate(this.server,this.name);
		}
		if(template != null && this.server == null){
			this.server = template.getSolrServer();
		}
		if(coreFactory == null  && this.server != null){
			coreFactory = new MulticoreSolrServerFactory(server);
		}
		if(factory == null  && this.server != null){
			factory = new HttpSolrServerFactory(server);
		}
	}
	
	
	@Override
	public boolean open(Object _address) {
		String address = _address.toString();
		if(StringUtils.isBlank(address)){
			throw new DataException("服务地址不能为空！");
		}
		// 是否为solrcloud
		boolean isCloud = false;
		if (!address.startsWith("http:")) {
			isCloud = true;
		}
		this.close();
		if (isCloud) {
			logger.info("...准备打开solrcloud服务【" + address + "】...");
			this.server = this.getCloudSolrServer(address, DEFAULT_COLLECTION);
			logger.info("...已打开solrcloud服务...");
		} else {
			logger.info("...准备打开solr服务【" + address + "】...");
			this.server = new HttpSolrServer(address);
			logger.info("...已打开solr服务...");
		}
		this.closed = false;
		this.address = address;
		init();
		return false;
	}

	/**
	 * 获取solrcloud
	 * 
	 * @author ls 2014年11月21日下午3:34:12
	 * @param zkHost
	 * @param defaultCollection
	 * @return
	 */
	protected CloudSolrServer getCloudSolrServer(String zkHost,
			String defaultCollection) {
		synchronized (this) {
			if (server == null) {
				ModifiableSolrParams params = new ModifiableSolrParams();
				params.set(HttpClientUtil.PROP_MAX_CONNECTIONS, 1000);// 10
				params.set(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, 500);// 5
				HttpClient client = HttpClientUtil.createClient(params);
				LBHttpSolrServer lbServer = new LBHttpSolrServer(client);

				CloudSolrServer server1 = null;
				server1 = new CloudSolrServer(zkHost, lbServer, true);
				server1.setDefaultCollection(defaultCollection);
				server1.setZkClientTimeout(30000);
				server1.setZkConnectTimeout(30000);
				server1.getLbServer().setConnectionTimeout(30000);
				if (defaultCollection != null) {
					server1.setDefaultCollection(defaultCollection);
				}
				server1.setParallelUpdates(true);
				server1.setDefaultCollection(DEFAULT_COLLECTION);
				server1.connect();
				this.server = server1;
			}
		}
		return (CloudSolrServer) server;
	}


	@Override
	public boolean commit(boolean softCommit) {
		boolean flag = false;
		logger.info("提交请求...");
		long start = System.currentTimeMillis();
		try {
			if(softCommit){
				this.server.commit();
			}else{
				this.server.commit(false, true, true);
			}
			flag = true;
		} catch (SolrServerException e) {
			throw new DataException(e.getMessage(), e);
		} catch (IOException e) {
			throw new DataException(e.getMessage(), e);
		}
		this.logger.info("提交请求操作耗时：" + (System.currentTimeMillis() - start)
				+ " ms");
		return flag;
	}
	
	
	@Override
	public boolean rollback() {
		boolean flag = false;
		logger.info("回滚请求...");
		long start = System.currentTimeMillis();
		try {
			this.server.rollback();
			flag = true;
		} catch (SolrServerException e) {
			throw new DataException(e.getMessage(), e);
		} catch (IOException e) {
			throw new DataException(e.getMessage(), e);
		}
		this.logger.info("回滚请求操作耗时：" + (System.currentTimeMillis() - start)
				+ " ms");
		return flag;
	}

	@Override
	public boolean insert(Insert insert, final boolean checkExist) {
		boolean flag = false;
		if (insert == null) {
			return false;
		}
		long start = System.currentTimeMillis();
		final Table table = insert.getTable()==null?DEFAULT_TABLE:insert.getTable();
		final Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
		final List<String> ids = new ArrayList<String>();
		int recordSize = 0;
		recordSize = insert.accept(new RecordVisitor() {
			@Override
			public void visit(Record record) {
				final SolrInputDocument doc = new SolrInputDocument();
				record.accept(new FieldVisitor() {
					@Override
					public void visit(Column column) {
						if (column.getName().equals(getPrikeyName())) {
							String id = "" ,srcId=  "";
							srcId = String.valueOf(column.getValue());
							id = IDHander.generateValue(table, srcId);
							if (checkExist && isExist(id)) {
								ids.add(id);
								throw new DataException("唯一键值【" + srcId
										+ "】已经存在！本次添加的数据添加失败!");
							}
							doc.addField(column.getName(), id,
									column.getBoost());
						}else{
							doc.addField(column.getName(), column.getValue(),
									column.getBoost());
						}
					}
				});
				doc.addField(DEFAULT_TABLE_FIELD, table.getFullName());
				docs.add(doc);
			}
		}); 
		
		flag = this.add(docs);
		this.logger.info("添加"+recordSize+"条记录耗时：" + (System.currentTimeMillis() - start)
				+ " ms");
		return flag;
	}

	/**
	 * 该主键值是否存在
	 * 
	 * @author ls 2014年11月21日下午5:35:48
	 * @param priKey
	 * @return
	 */
	protected boolean isExist(String priKey) {
		boolean flag = false;
		long start = System.currentTimeMillis();
		SolrQuery query = new SolrQuery("*:*");
		query.addFilterQuery(this.getPrikeyName() + ":" + priKey);
		query.setFacet(true);
		query.setStart(0);
		query.setRows(0);
		try {
			this.logger.info("检查存在性条件：" + query);
			QueryResponse response = server.query(query);
			long count = response.getResults().getNumFound();
			if (count > 0) {
				flag = true;
			}
		} catch (Exception e) {
			flag = false;
			throw new DataException(e.getMessage(), e);
		}
		this.logger.info("检查存在性耗时：" + (System.currentTimeMillis() - start)
				+ " ms");
		return flag;
	}

	
	protected String makeIn(Collection<String> values) {
		StringBuilder sb = new StringBuilder();
		if (values != null) {
			int i = 0;
			for (Object string : values) {
				if (string != null && !"".equals(string)) {
					if (i == 0) {
						sb.append("(");
					}
					if (i > 0) {
						sb.append(" ");
					}
					sb.append(string);
					i++;
				}
			}
			if (i > 0) {
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
	
	protected boolean isExist(Collection<String> priKeys) {
		boolean flag = false;
		if(priKeys==null){
			return flag;
		}
		long start = System.currentTimeMillis();
		SolrQuery query = new SolrQuery("*:*");
		query.addFilterQuery(this.getPrikeyName() + ":" + this.makeIn(priKeys));
		query.setFacet(true);
		query.setStart(0);
		query.setRows(0);
		try {
			this.logger.info("检查存在性条件：" + query);
			QueryResponse response = server.query(query);
			long count = response.getResults().getNumFound();
			if (count > 0) {
				flag = true;
			}
		} catch (Exception e) {
			flag = false;
			throw new DataException(e.getMessage(), e);
		}
		this.logger.info("检查存在性耗时：" + (System.currentTimeMillis() - start)
				+ " ms");
		return flag;
	}
	
	
	@Override
	public boolean delete(Delete delete) {
		if (delete == null) {
			return false;
		}
		long start = System.currentTimeMillis();
		boolean flag = false;
		if(delete.hasWhere()){
			final Table table = delete.getTable()==null?DEFAULT_TABLE:delete.getTable();
			delete.addFromItem(table);
		}
		DeleteParser parser = new DeleteParser(delete);
		SolrQuery query = parser.getQuery();
		boolean hasCondition = parser.hasCondition();
		try {
			if (delete.hasId()) {
				final Table table = delete.getTable()==null?DEFAULT_TABLE:delete.getTable();
				List<String> ids = delete.getIds();
				List<String> idstrs = new ArrayList<String>();
				for (String string : ids) {
					idstrs.add(IDHander.generateValue(table, string));
				}
				hasCondition = true;
				this.logger.info("删除：" + idstrs);
				this.server.deleteById(idstrs, DEFAULT_COMMIT_MS);
			}
			if(parser.hasCondition() || !hasCondition){
				this.logger.info(query.getQuery());
				this.server.deleteByQuery(query.getQuery(), DEFAULT_COMMIT_MS);
			}
			flag = true;
		} catch (SolrServerException e) {
			throw new DataException(e.getMessage(), e);
		} catch (IOException e) {
			throw new DataException(e.getMessage(), e);
		} finally {
			if (this.autoCommit) {
				if (flag) {
					this.commit();
				} else {
					this.rollback();
				}
			}
		}
		this.logger.info("删除操作耗时：" + (System.currentTimeMillis() - start)
				+ " ms");
		return flag;
	}

	@Override
	public boolean update(Update update, final boolean checkExist) {
		if (update == null) {
			return false;
		}
		boolean flag = false;
		long start = System.currentTimeMillis();
		List<Record> list = update.getRecords();
		final Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
		final Table table = update.getTable()==null?DEFAULT_TABLE:update.getTable();
		long recordSize = 0;
		if (update.hasWhere()) {
			if (list == null || list.isEmpty()) {
				return false;
			}
			Select select = new Select();
			select.addFromItem(table);
			select.setWhere(update.getWhere());
			final Record updateRecord = list.get(0);
			try {
				recordSize = this.scan(select, new ScanRowMapperAdapter(this.getPrikeyName()) {
					
					@Override
					public boolean processingOfResults(List<Record> list, int page) {
						if(list!=null){
							 for (Record record : list) {
								 final SolrInputDocument doc = new SolrInputDocument();
								 record.accept(new FieldVisitor() {
									
									@Override
									public void visit(Column column) {
										if (column.getName().equals(getPrikeyName())) {
											String id = "",srcId = "";
											srcId = String.valueOf(column.getValue());
											id = IDHander.generateValue(table, srcId);
											if (checkExist && !isExist(id)) {
												throw new DataException("唯一键值【" + srcId + "】不存在！更新失败!");
											}
											doc.addField(column.getName(), id,
													column.getBoost());
										}else{
											doc.addField(column.getName(), column.getValue(),
												column.getBoost());
										}
										
									}
								});
								
								 updateRecord.accept(new FieldVisitor() {
									@Override
									public void visit(Column column) {
										if (column.getName().equals(getPrikeyName())) {
											String id = "",srcId = "";
											srcId = String.valueOf(column.getValue());
											id = IDHander.generateValue(table, srcId);
											if (checkExist && !isExist(id)) {
												throw new DataException("唯一键值【" + srcId + "】不存在！更新失败!");
											}
											doc.setField(column.getName(), id,
													column.getBoost());
										}else{
											doc.setField(column.getName(), column.getValue(),
													column.getBoost());
										}
									}
								});
								docs.add(doc);
							 }
						}
						return true;
					}
				});
			} catch (SQLException e) {
				throw new DataException(e.getMessage(), e);
			}
		} else {
			recordSize = update.accept(new RecordVisitor() {
				
				@Override
				public void visit(Record record) {
					final SolrInputDocument doc = new SolrInputDocument();
					record.accept(new FieldVisitor() {
						@Override
						public void visit(Column column) {
							if (column.getName().equals(getPrikeyName())) {
								String id = "",srcId = "";
								srcId = String.valueOf(column.getValue());
								id = IDHander.generateValue(table, srcId);
								if (checkExist && !isExist(id)) {
									throw new DataException("唯一键值【" + srcId + "】不存在！更新失败!");
								}
								doc.addField(column.getName(), id,
										column.getBoost());
							}else{
								doc.addField(column.getName(), column.getValue(),
										column.getBoost());
							}
						}
					});
					doc.addField(DEFAULT_TABLE_FIELD, table.getFullName());
					docs.add(doc);
				}
			});
		}
		flag = this.add(docs); 
		this.logger.info("更新"+recordSize+"条记录耗时：" + (System.currentTimeMillis() - start)
				+ " ms");
		return flag;
	}
	
	
	/**
	 * 添加到服务
	 * @author ls
	 * 2014年11月25日下午7:46:24 
	 * @param docs
	 * @return
	 */
	private boolean add(final Collection<SolrInputDocument> docs){
		boolean flag = false;
		try {
			if (this.logger.isDebugEnabled()) {
				this.logger.debug(docs.toString());
			}
			if(!docs.isEmpty()){
				flag = this.server.add(docs).getStatus()==200;
			}
		} catch (SolrServerException e) {
			throw new DataException(e.getMessage(), e);
		} catch (IOException e) {
			throw new DataException(e.getMessage(), e);
		} finally {
			if (this.autoCommit) {
				if (flag) {
					this.commit();
				} else {
					this.rollback();
				}
			}
		}
		return flag;
	}

	@Override
	public DataResult search(Select select) {
		if (select == null) {
			return new DataResult();
		}
		long start = System.currentTimeMillis();
		final Table table = select.getTable()==null?DEFAULT_TABLE:select.getTable();
		select.addFromItem(table);
		SelectParser parser = new SelectParser(select);
		SolrQuery query = parser.getQuery();
		if(!select.hasWhere()){
			if(table != null){
				query.addFilterQuery(DEFAULT_TABLE_FIELD+":"+table.getFullName());
			}
		}
		QueryResponse response = null;
		this.logger.info(query.toString());
		try {
			response = this.server.query(query);
		} catch (SolrServerException e) {
			throw new DataException(e.getMessage(), e);
		}
		DataResult result = null;
		if (select.isGroup) {
			result = DataResultMaker.makeGroupResult(
					response.getGroupResponse(), select,this.getPrikeyName());
		} else {
			result = DataResultMaker
					.makeResult(response.getResults(), select,this.getPrikeyName());
		}
		this.logger.info("查询操作耗时：" + (System.currentTimeMillis() - start)
				+ " ms");
		return result;
	}

	@Override
	public void close() {
		if (this.server != null) {
			logger.info("关闭solr服务...");
			try {
				this.server.shutdown();
			} catch (Exception e) {
				e.printStackTrace();
			}
			server = null;
		}
		this.closed = true;
	}

	@Override
	public void execute(String sql) {
		this.execute(sql, false);
	}

	@Override
	public void execute(String sql, boolean checkExist) {
		// TODO Auto-generated method stub
		short type = 0;// 0:add,1:update,2:delete
		if (type == 0) {
			Insert insert = null;
			this.insert(insert, checkExist);
		} else if (type == 1) {
			Update update = null;
			this.update(update);
		} else {
			Delete delete = null;
			this.delete(delete);
		}
	}

	@Override
	public long executeQueryForInt(String sql) throws SQLException {
		// TODO Auto-generated method stub
		Select select = null;
		return this.executeQueryForInt(select);
	}

	@Override
	public List<?> executeQuery(String sql) {
		// TODO Auto-generated method stub
		 return null;
	}

	@Override
	public long executeQueryForInt(Select select) throws SQLException {
		int count = 0;
		DataResultSet result = this.executeQuery(select);
		int line = 0;
		int index = 0;
		while (result.next()) {
			count += result.getInt(AFunctions.COUNT.getName());
			line++;
			index++;
			if (index == 2) {
				line = index;
			} else if (index == 1) {
				if (count > 0) {
					line = 0;
				}
			}
			// count = result.getInt(0);
		}
		return Math.max(count, line);
	}

	/**
	 * 使用游标
	 * @author ls
	 * 2014年11月25日下午7:19:59 
	 * @param query
	 * @param rowCount
	 * @param rowMapper
	 * @throws SolrServerException
	 * @throws SQLException
	 */
	private <T> long cursor(SolrQuery query, int rowCount, ScanRowMapper<T> rowMapper)
			throws SolrServerException, SQLException {
		SolrQuery q = query.setRows(rowCount);
		List<SortClause> list = q.getSorts();
		boolean exist = false;
		if(list != null){
			for (SortClause sortClause : list) {
				if(sortClause.getItem().equals(this.getPrikeyName())){
					exist = true;
					break;
				}
			}
		}
		if(!exist){
			q.addSort(SortClause.desc(this.getPrikeyName()));
		}
		String cursorMark = CursorMarkParams.CURSOR_MARK_START;
		boolean done = false;
		int page = 1;
		boolean showField = true;
		long count = 0, showCount = 0;
		String fl = query.get("fl");
		while (!done) {
			if(rowMapper != null){
				showField = rowMapper.showField(page);
			}
			if (!showField) {
				q.set("fl", this.getPrikeyName());
			}else{
				q.set("fl", fl);
			}
			long start = System.currentTimeMillis();
			q.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
			this.logger.info(q.toString());
			QueryResponse rsp = server.query(q);
			count = rsp.getResults().getNumFound();
			this.logger.info("本次遍历" + (page) + "操作耗时："
					+ (System.currentTimeMillis() - start) + " ms");
			boolean goon = this.processingOfResults(rsp, showField, page,
					rowMapper);
			String nextCursorMark = rsp.getNextCursorMark();
			showCount = rowCount * page;
			if (showCount >= count || !goon
					|| cursorMark.equals(nextCursorMark)) {
				done = true;
			}
			++page;
			cursorMark = nextCursorMark;
		}
		return count;
	}

	
	/**
	 * 处理结果
	 * @author ls
	 * 2014年11月25日下午7:19:49 
	 * @param rsp
	 * @param showField
	 * @param page
	 * @param rowMapper
	 * @return
	 * @throws SQLException
	 */
	private <T> boolean processingOfResults(QueryResponse rsp, boolean showField,
			int page, ScanRowMapper<T> rowMapper) throws SQLException {
		List<T> list = null;
		if (showField) {
			DataResult result = DataResultMaker.makeResult(
					rsp.getResults(), null,this.getPrikeyName());
			if(rowMapper != null){
				DataResultSet resultSet = new DataResultSetImpl(result);
				list = new ArrayList<T>(resultSet.getFetchSize());
				int index = 1;
				while (resultSet.next()) {
					T obj = rowMapper.mapRow(resultSet,index++);
					if (obj != null) {
						list.add(obj);
					}
				}
				resultSet.close();
			}else{
				list = (List<T>) result.getResults();
			}
		}
		boolean goon = true;
		if(rowMapper != null){
			goon = rowMapper.processingOfResults(list, page);
		}
		return goon;
	}

	@Override
	public <T> long scan(Select select, ScanRowMapper<T> rowMapper)
			throws SQLException {

		long start = System.currentTimeMillis();
		final Table table = select.getTable()==null?DEFAULT_TABLE:select.getTable();
		select.addFromItem(table);
		SelectParser parser = new SelectParser(select);
		SolrQuery query = parser.getQuery();
		long count = 0;
		try {
			count =  this.cursor(query, select.getRowCount(), rowMapper);
		} catch (SolrServerException e) {
			throw new SQLException(e.getMessage(), e);
		}
		this.logger.info("遍历"+count+"条记录耗时：" + (System.currentTimeMillis() - start)
				+ " ms");
		return count;
	}

	@Override
	public void optimize() {
		long start = System.currentTimeMillis();
		try {
			this.server.optimize(false, false);
		} catch (SolrServerException e) {
			throw new DataException(e.getMessage(), e);
		} catch (IOException e) {
			throw new DataException(e.getMessage(), e);
		}
		this.logger.info("优化数据耗时：" + (System.currentTimeMillis() - start)
				+ " ms");
	}

	@Override
	public SolrServer getServer() {
		return this.server;
	}

	@Override
	public long load(Table fromTable, Select select, final Table toTable, boolean deleteOldData,
			ScanRowMapper<Record> rowMapper) throws SQLException {
		long recordSize = 0;
		if(fromTable != null){
			select.addFromItem(fromTable);
		}
		Table t = select.getTable()!=null?select.getTable():fromTable;
		final Table table = t==null?DEFAULT_TABLE:t;
		long start = System.currentTimeMillis();
		if(select.getTable() == null){
			select.addFromItem(table);
		}
		final Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
		try {
			recordSize = this.scan(select, new ScanRowMapperAdapter(this.getPrikeyName()) {
				
				@Override
				public boolean processingOfResults(List<Record> list, int page) {
					if(list!=null){
						 for (Record record : list) {
							 final SolrInputDocument doc = new SolrInputDocument();
							 record.accept(new FieldVisitor() {
								
								@Override
								public void visit(Column column) {
									if (column.getName().equals(getPrikeyName())) {
										String id = "",srcId = "";
										srcId = String.valueOf(column.getValue());
										id = IDHander.generateValue(toTable, srcId);
										doc.addField(column.getName(), id,
												column.getBoost());
									}else{
										doc.addField(column.getName(), column.getValue(),
											column.getBoost());
									}
									
								}
							});
							doc.setField(DEFAULT_TABLE_FIELD, toTable.getFullName());
							docs.add(doc);
						 }
					}
					return true;
				}
			});
			this.add(docs); 
			if(deleteOldData && !table.equals(toTable)){
				this.commit(true);
				Delete delete = new Delete();
				delete.addFromItem(table);
				delete.setWhere(select.getWhere());
				this.delete(delete);
			}
		} catch (SQLException e) {
			throw new DataException(e.getMessage(), e);
		}
		this.logger.info("导入"+recordSize+"条记录耗时：" + (System.currentTimeMillis() - start)
				+ " ms");
		return recordSize;
	}

	
	@Override
	public SolrTemplate getTemplate() {
		return template;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setServer(SolrServer server) {
		this.server = server;
		this.init();
	}

	public void setTemplate(SolrTemplate template) {
		this.template = template;
		this.init();
		
	}
 
	public MulticoreSolrServerFactory getCoreFactory() {
		return coreFactory;
	}

	public void setCoreFactory(MulticoreSolrServerFactory coreFactory) {
		this.coreFactory = coreFactory;
	}

	public HttpSolrServerFactory getFactory() {
		return this.factory;
	}

	public void setFactory(HttpSolrServerFactory factory) {
		this.factory = factory;
	}
	 
}
